package com.asa.common.common

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.content.FileProvider
import java.io.File
import java.lang.Exception

object AppUtils {


    /**
     *  获取app版本号
     */
    fun getVersionCode(context: Context):Int{
        var version = 0
        try {
            version =context.packageManager.getPackageInfo(context.packageName,0).versionCode
        }catch (e:Exception){
            e.printStackTrace()
        }
        return version
    }


    /**
     *  获取apk版本名
     */
    fun getVersionName(context: Context):String{
        var versionName = ""
        try {
            versionName = context.packageManager.getPackageInfo(context.packageName,0).versionName
        }catch (e:Exception){
            e.printStackTrace()
        }
        return versionName
    }


    fun installApk(context: Context,filePath:String){
        val apkFile= File(filePath)
        val intent = Intent(Intent.ACTION_VIEW)
        intent.flags =Intent.FLAG_ACTIVITY_NEW_TASK
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.N){
            intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
            val contentUri = FileProvider.getUriForFile(context, "com.company.reading.Provider", apkFile)
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
        } else{
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive")
        }
        context.startActivity(intent)
    }


    fun getPackageName(context: Context):String = context.packageName




}